home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Unix / net⁄if.h < prev    next >
Encoding:
Text File  |  1988-03-18  |  7.3 KB  |  225 lines  |  [TEXT/MACA]

  1. /*    @(#)if.h 1.1 86/07/07 SMI; from UCB 6.2 83/08/28    */
  2.  
  3. #ifndef __IF_HEADER__
  4. #define __IF_HEADER__
  5.  
  6. /*
  7.  * Structures defining a network interface, providing a packet
  8.  * transport mechanism (ala level 0 of the PUP protocols).
  9.  *
  10.  * Each interface accepts output datagrams of a specified maximum
  11.  * length, and provides higher level routines with input datagrams
  12.  * received from its medium.
  13.  *
  14.  * Output occurs when the routine if_output is called, with three parameters:
  15.  *    (*ifp->if_output)(ifp, m, dst)
  16.  * Here m is the mbuf chain to be sent and dst is the destination address.
  17.  * The output routine encapsulates the supplied datagram if necessary,
  18.  * and then transmits it on its medium.
  19.  *
  20.  * On input, each interface unwraps the data received by it, and either
  21.  * places it on the input queue of a internetwork datagram routine
  22.  * and posts the associated software interrupt, or passes the datagram to a raw
  23.  * packet input routine.
  24.  *
  25.  * Routines exist for locating interfaces by their addresses
  26.  * or for locating a interface on a certain network, as well as more general
  27.  * routing and gateway routines maintaining information used to locate
  28.  * interfaces.  These routines live in the files if.c and route.c
  29.  */
  30.  
  31. /*
  32.  * Structure defining a queue for a network interface.
  33.  *
  34.  * (Would like to call this struct ``if'', but C isn't PL/1.)
  35.  *
  36.  * EVENTUALLY PURGE if_net AND if_host FROM STRUCTURE
  37.  */
  38. struct ifnet {
  39.     char    *if_name;        /* name, e.g. ``en'' or ``lo'' */
  40.     short    if_unit;        /* sub-unit for lower level driver */
  41.     short    if_mtu;            /* maximum transmission unit */
  42.     int    if_net;            /* network number of interface */
  43.     short    if_flags;        /* up/down, broadcast, etc. */
  44.     short    if_timer;        /* time 'til if_watchdog called */
  45.     int    if_host[2];        /* local net host number */
  46.     struct    sockaddr if_addr;    /* address of interface */
  47.     union {
  48.         struct    sockaddr ifu_broadaddr;
  49.         struct    sockaddr ifu_dstaddr;
  50.     } if_ifu;
  51. #define    if_broadaddr    if_ifu.ifu_broadaddr    /* broadcast address */
  52. #define    if_dstaddr    if_ifu.ifu_dstaddr    /* other end of p-to-p link */
  53.     struct    ifqueue {
  54.         struct    mbuf *ifq_head;
  55.         struct    mbuf *ifq_tail;
  56.         int    ifq_len;
  57.         int    ifq_maxlen;
  58.         int    ifq_drops;
  59.     } if_snd;            /* output queue */
  60. /* procedure handles */
  61.     int    (*if_init)();        /* init routine */
  62.     int    (*if_output)();        /* output routine */
  63.     int    (*if_ioctl)();        /* ioctl routine */
  64.     int    (*if_reset)();        /* bus reset routine */
  65.     int    (*if_watchdog)();    /* timer routine */
  66. /* generic interface statistics */
  67.     int    if_ipackets;        /* packets received on interface */
  68.     int    if_ierrors;        /* input errors on interface */
  69.     int    if_opackets;        /* packets sent on interface */
  70.     int    if_oerrors;        /* output errors on interface */
  71.     int    if_collisions;        /* collisions on csma interfaces */
  72. /* end statistics */
  73.     struct    ifnet *if_next;
  74.     struct    ifnet *if_upper;    /* next layer up */
  75.     struct    ifnet *if_lower;    /* next layer down */
  76.     int    (*if_input)();        /* input routine */
  77.     int    (*if_ctlin)();        /* control input routine */
  78.     int    (*if_ctlout)();        /* control output routine */
  79. #ifdef sun
  80.     struct map *if_memmap;        /* rmap for interface specific memory */
  81. #endif
  82. };
  83.  
  84. #define    IFF_UP        0x1        /* interface is up */
  85. #define    IFF_BROADCAST    0x2        /* broadcast address valid */
  86. #define    IFF_DEBUG    0x4        /* turn on debugging */
  87. #define    IFF_ROUTE    0x8        /* routing entry installed */
  88. #define    IFF_POINTOPOINT    0x10        /* interface is point-to-point link */
  89. #define    IFF_NOTRAILERS    0x20        /* avoid use of trailers */
  90. #define    IFF_RUNNING    0x40        /* resources allocated */
  91. #define    IFF_NOARP    0x80        /* no address resolution protocol */
  92. #define IFF_PROMISC    0x100        /* run in promiscuous mode */
  93.  
  94. /*
  95.  * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1)
  96.  * input routines have queues of messages stored on ifqueue structures
  97.  * (defined above).  Entries are added to and deleted from these structures
  98.  * by these macros, which should be called with ipl raised to splimp().
  99.  */
  100. #define    IF_QFULL(ifq)        ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
  101. #define    IF_DROP(ifq)        ((ifq)->ifq_drops++)
  102. #define    IF_ENQUEUE(ifq, m) { \
  103.     (m)->m_act = 0; \
  104.     if ((ifq)->ifq_tail == 0) \
  105.         (ifq)->ifq_head = m; \
  106.     else \
  107.         (ifq)->ifq_tail->m_act = m; \
  108.     (ifq)->ifq_tail = m; \
  109.     (ifq)->ifq_len++; \
  110. }
  111. #define    IF_PREPEND(ifq, m) { \
  112.     (m)->m_act = (ifq)->ifq_head; \
  113.     if ((ifq)->ifq_tail == 0) \
  114.         (ifq)->ifq_tail = (m); \
  115.     (ifq)->ifq_head = (m); \
  116.     (ifq)->ifq_len++; \
  117. }
  118. /*
  119.  * Packets destined for level-1 protocol input routines
  120.  * have a pointer to the receiving interface prepended to the data.
  121.  * (Actually, this isn't true yet, but eventually will be.)
  122.  * IF_DEQUEUEIF extracts and returns this pointer when dequeueing the packet.
  123.  * IF_ADJ should be used otherwise to adjust for its presence.
  124.  */
  125. #define    IF_ADJ(m) { \
  126.     (m)->m_off += sizeof(struct ifnet *); \
  127.     (m)->m_len -= sizeof(struct ifnet *); \
  128.     if ((m)->m_len == 0) { \
  129.         struct mbuf *n; \
  130.         MFREE((m), n); \
  131.         (m) = n; \
  132.     } \
  133. }
  134. #define    IF_DEQUEUEIF(ifq, m, ifp) { \
  135.     (m) = (ifq)->ifq_head; \
  136.     if (m) { \
  137.         if (((ifq)->ifq_head = (m)->m_act) == 0) \
  138.             (ifq)->ifq_tail = 0; \
  139.         (m)->m_act = 0; \
  140.         (ifq)->ifq_len--; \
  141.         (ifp) = *(mtod((m), struct ifnet **)); \
  142.         IF_ADJ(m); \
  143.     } \
  144. }
  145. #define    IF_DEQUEUE(ifq, m) { \
  146.     (m) = (ifq)->ifq_head; \
  147.     if (m) { \
  148.         if (((ifq)->ifq_head = (m)->m_act) == 0) \
  149.             (ifq)->ifq_tail = 0; \
  150.         (m)->m_act = 0; \
  151.         (ifq)->ifq_len--; \
  152.     } \
  153. }
  154.  
  155. #define    IFQ_MAXLEN    50
  156. #define    IFNET_SLOWHZ    1        /* granularity is 1 second */
  157.  
  158. /*
  159.  * Interface request structure used for socket
  160.  * ioctl's.  All interface ioctl's must have parameter
  161.  * definitions which begin with ifr_name.  The
  162.  * remainder may be interface specific.
  163.  */
  164. struct    ifreq {
  165. #define    IFNAMSIZ    16
  166.     char    ifr_name[IFNAMSIZ];        /* if name, e.g. "en0" */
  167.     union {
  168.         struct    sockaddr ifru_addr;
  169.         struct    sockaddr ifru_dstaddr;
  170.         char    ifru_oname[IFNAMSIZ];    /* other if name */
  171.         short    ifru_flags;
  172.         char    ifru_data[1];        /* interface dependent data */
  173.     } ifr_ifru;
  174. #define    ifr_addr    ifr_ifru.ifru_addr    /* address */
  175. #define    ifr_dstaddr    ifr_ifru.ifru_dstaddr    /* other end of p-to-p link */
  176. #define    ifr_oname    ifr_ifru.ifru_oname    /* other if name */
  177. #define    ifr_flags    ifr_ifru.ifru_flags    /* flags */
  178. #define    ifr_data    ifr_ifru.ifru_data    /* for use by interface */
  179. };
  180.  
  181. /*
  182.  * Structure used in SIOCGIFCONF request.
  183.  * Used to retrieve interface configuration
  184.  * for machine (useful for programs which
  185.  * must know all networks accessible).
  186.  */
  187. struct    ifconf {
  188.     int    ifc_len;        /* size of associated buffer */
  189.     union {
  190.         caddr_t    ifcu_buf;
  191.         struct    ifreq *ifcu_req;
  192.     } ifc_ifcu;
  193. #define    ifc_buf    ifc_ifcu.ifcu_buf    /* buffer address */
  194. #define    ifc_req    ifc_ifcu.ifcu_req    /* array of structures returned */
  195. };
  196.  
  197. /*
  198.  * ARP ioctl request
  199.  */
  200. struct arpreq {
  201.     struct sockaddr    arp_pa;        /* protocol address */
  202.     struct sockaddr    arp_ha;        /* hardware address */
  203.     int    arp_flags;        /* flags */
  204. };
  205. /*  arp_flags and at_flags field values */
  206. #define    ATF_INUSE    1    /* entry in use */
  207. #define ATF_COM        2    /* completed entry (enaddr valid) */
  208. #define    ATF_PERM    4    /* permanent entry */
  209. #define    ATF_PUBL    8    /* publish entry (respond for other host) */
  210.  
  211. #ifdef KERNEL
  212. #ifdef INET
  213. struct    ifqueue    ipintrq;        /* ip packet input queue */
  214. #endif
  215. struct    ifqueue rawintrq;        /* raw packet input queue */
  216. struct    ifnet *ifnet;
  217. struct    ifnet *if_ifwithaddr(), *if_ifwithnet(), *if_ifwithaf();
  218. struct    ifnet *if_ifwithafup();
  219. struct    ifnet *if_ifonnetof(), *if_ifwithdstaddr();
  220. struct    ifnet *ifunit();
  221. struct    in_addr if_makeaddr();
  222. #endif
  223.  
  224. #endif !__IF_HEADER__
  225.